<template>
  <c-form-dialog
    ref="form"
    :visible.sync="innerVisible"
    :form-items="formItems"
    :form-data="editData"
    :table-name="tableName"
    :table-id="tableId"
    :auto-close="false"
    @close="handleClose"
    @success="handleSuccess"
    submitText="生成订单"
    :before-save="handleBeforeSave"
  ></c-form-dialog>
</template>

<script>
import { mapGetters } from 'vuex'
import { enumPayMethods } from '@/credit/views/union/settle/enum'
export default {
  components: {},
  props: {
    visible: Boolean,
    tableId: String,
    formData: {
      type: Object,
      default() {
        return {}
      },
    },
  },
  data() {
    const isNumberValidator = (rule, value, callback) => {
      if (!value) {
        callback()
      } else if (!Number.isFinite(+value)) {
        callback(new Error('请输入正确的数值'))
      } else {
        callback()
      }
    }
    return {
      innerVisible: false,
      tableName: '',
      formItems: [
        {
          tag: 'input',
          itemAttrs: {
            label: '实际缴纳人',
            rules: [{ required: true, message: '请输入名称', trigger: 'blur' }],
          },
          attrs: { key: 'actualPayer', value: '', placeholder: '请输入名称' },
        },
        {
          tag: 'number',
          itemAttrs: {
            label: '本次订单金额',
            rules: [
              { validator: isNumberValidator, trigger: 'blur' },
              { required: true, message: '请输入缴费金额', trigger: 'blur' },
            ],
          },
          attrs: { key: 'amount', value: '', placeholder: '请输入本次缴费金额' },
          tagSlots: [{ name: 'append', slot: '元' }],
          appendSlot: 'appendSlot',
          directives: [
            {
              name: 'input-number',
              value: {
                min: 0,
                fixed: 2,
              },
            },
          ],
        },
        {
          tag: 'select',
          itemAttrs: {
            label: '支付方式',
            rules: [{ required: true, message: '请选择支付方式', trigger: 'change' }],
          },
          attrs: {
            key: 'payType',
            options: enumPayMethods,
            placeholder: '请选择支付方式',
          },
        },
      ],
      addressNames: '',
      guaMethodName: '',
      productFeatures: '',
      editData: {
        amount: '',
      },
    }
  },
  computed: {
    ...mapGetters(['user', 'clientId']),
  },
  watch: {
    visible(val) {
      this.editData.amount = this.formData.due_warrant_fee || ''
      this.innerVisible = val
    },
  },
  created() {},
  mounted() {},
  methods: {
    async handleBeforeSave(model) {
      model.submitPersonName = this.user.realName
      model.submitClientId = this.clientId
      model.orderNo = this.formData.order_no
      model.relatedId = this.formData.id
      const res = await this.$http.post('/pay/redirect', model)
      const resData = res?.data ?? {}
      if (resData.error) {
        this.$message.error(
          resData?.errorMessage ?? resData?.data?.description ?? '生成订单失败，请重试'
        )
      } else {
        this.$message.success('生成订单成功')
        this.handleSuccess()
        this.handleClose()
      }
      return false
    },
    handleClose() {
      this.$emit('update:visible', false)
    },
    handleSuccess(res) {
      this.$emit('success', res)
    },
  },
}
</script>
<style lang="scss" scoped></style>
